package com.sanron.sunweather.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Space;
import android.widget.TextView;

import com.sanron.sunweather.R;
import com.sanron.sunweather.common.IconManager;
import com.sanron.sunweather.entity.LifeIndex;
import com.sanron.sunweather.entity.WeatherData;
import com.viewpagerindicator.TitlePageIndicator;

/**
 * 生活指数
 * 
 * @author 三荣
 * 
 */
public class LifeIndexFrag extends BaseFragment {

	private View contentView;
	private ViewPager pager;
	private IndexPagerAdapater pageAdapter;
	private TextView tvCity;
	private TitlePageIndicator indicator;
	private List<ListView> views;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		views = new ArrayList<ListView>();
		if (weatherData != null && weatherData.getLifeIndexs()!=null){
			for (int i = 0; i < 3; i++) {
				ListView listView = new ListView(getActivity());
				listView.setVerticalScrollBarEnabled(false);
				IndexListAdapter adapter = new IndexListAdapter(weatherData
						.getLifeIndexs().get(i), getActivity());
				LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT);
				android.widget.AbsListView.LayoutParams lpHeader = new android.widget.AbsListView.LayoutParams(
						LayoutParams.MATCH_PARENT,
						-getResources().getDimensionPixelSize(
								R.dimen.pager_top_margin));
				android.widget.AbsListView.LayoutParams lpFooter = new android.widget.AbsListView.LayoutParams(
						LayoutParams.MATCH_PARENT,
						-getResources().getDimensionPixelSize(
								R.dimen.pager_top_margin)*3);
				Space header = new Space(getActivity());
				Space footer = new Space(getActivity());
				header.setLayoutParams(lpHeader);
				footer.setLayoutParams(lpFooter);
				listView.setScrollBarSize(0);
				listView.addHeaderView(header);
				listView.addFooterView(footer);
				listView.setDividerHeight(0);
				listView.setAdapter(adapter);
				listView.setLayoutParams(lp);
				views.add(listView);
			}
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		initViews(inflater);
		return contentView;
	}

	public void initViews(LayoutInflater inflater){
		contentView = inflater.inflate(R.layout.fragment_life_index, null);
		pager = (ViewPager) contentView.findViewById(R.id.pager_life_index);
		tvCity = (TextView) contentView.findViewById(R.id.tv_city);
		pageAdapter = new IndexPagerAdapater();
		pager.setAdapter(pageAdapter);
		indicator = (TitlePageIndicator) contentView.findViewById(R.id.pager_indicator);
		indicator.setViewPager(pager);
		updateViews();
	}
	
	public class IndexPagerAdapater extends PagerAdapter {

		public final String[] TITLES = new String[] { "今天", "明天", "后天" };

		@Override
		public int getCount() {
			return views.size();
		}
		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
			return views.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		}
	}

	public static class IndexListAdapter extends BaseAdapter {

		private Context context;
		private List<LifeIndex> lifeIndexs;

		public IndexListAdapter(List<LifeIndex> lifeIndexs, Context context) {
			this.context = context;
			this.lifeIndexs = lifeIndexs;
		}

		@Override
		public int getCount() {
			return lifeIndexs == null ? 0 : lifeIndexs.size();
		}

		@Override
		public Object getItem(int position) {
			return lifeIndexs.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LifeIndex lifeIndex = lifeIndexs.get(position);
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.list_item_lifeindex, null);
			}
			ImageView ivIndex = (ImageView) convertView
					.findViewById(R.id.iv_index);
			TextView tvIndexName = (TextView) convertView
					.findViewById(R.id.tv_index_name);
			TextView tvIndexValue = (TextView) convertView
					.findViewById(R.id.tv_index_value);
			TextView tvDetail = (TextView) convertView
					.findViewById(R.id.tv_index_detail);
			tvIndexName.setText(lifeIndex.getName()+" :");
			tvIndexValue.setText(" "+lifeIndex.getValue());
			tvDetail.setText(lifeIndex.getDetail());
			Integer icon = IconManager.getLifeIndexIcon(lifeIndex.getName());
			if (icon != null) {
				ivIndex.setImageResource(icon);
			}
			return convertView;
		}
	}

	@Override
	protected void updateViews() {
		tvCity.setText(weatherData.getCity()+" 生活指数");
		for(int i=0; i<3; i++){
			views.get(i).setAdapter(new IndexListAdapter(weatherData
					.getLifeIndexs().get(i), getActivity()));
		}
		pageAdapter.notifyDataSetChanged();
	}
}
